let modalContent = $('#modalContent');
let groupInput = $('#groupInput');
let importExcelModal = $('#importExcelModal');

let industryId = 1;

$(function () {
    excelModalInit();
});

function importExcelModalShowOrHide(flag) {
    importExcelModal.modal(flag ? "show" : "hide");
}

function excelModalInit() {
    $("#excelFile").fileinput({
        uploadUrl: "/api/excel/import",//上传的地址
        uploadAsync: true,              //异步上传
        language: "zh",                 //设置语言
        showCaption: true,              //是否显示标题
        showUpload: true,               //是否显示上传按钮
        showRemove: true,               //是否显示移除按钮
        showPreview: false,             //是否显示预览按钮
        browseClass: "btn btn-default", //按钮样式
        dropZoneEnabled: false,         //是否显示拖拽区域
        allowedFileExtensions: ["xlsx", "xls"], //接收的文件后缀["xls", "xlsx"]
        maxFileCount: 1,                        //最大上传文件数限制
        uploadClass: "btn btn-default",
        allowedPreviewTypes: null,
        //传递额外参数的回调函数, 会在上传之前调用
        uploadExtraData: function(previewId, index) {
            let params = {};
            params.industryId = industryId;
            return params;
        }
    }).on('fileuploaded', function (event, data, previewId, index) {  //文件上传完成后的事件, 也就是点击上传时触发
        //这里必须使用eval函数
        let response = eval(data.response);
        let code = response.code;
        if (code === '200') {
            //$("#gridImport").bootstrapTable("load", response.errorList);
            swal({
                title: "成功",
                text: response.message,
                type: "success"
            });
        } else {
            swal({
                title: "错误",
                text: response.message,
                type: "error"
            });
        }
    }).on("filebatchselected", function (event, files) {//该事件在选择一个文件之后触发
        let data = new FormData($("#ffImport")[0]);
        data.append("industryId", 123);
        $.ajax({
            url: '/api/excel/import/verify',
            data: data,
            type: "POST",
            cache: false,//控制上传文件是否缓存
            processData: false,//对数据进行序列化处理 必须fasle
            contentType: false,//必须
            success: function (response) {
                console.log("审查excel的响应为: ", response);
                let code = response.code;
                if (code === '200') {
                    //$("#gridImport").bootstrapTable("load", data.errorList);
                    swal({
                        title: "成功",
                        text: response.message,
                        type: "success"
                    });
                } else {
                    swal({
                        title: "错误",
                        text: response.message,
                        type: "error"
                    });
                }
            }
        });
    });
}